<template>
  <div>
    <article class="main-breadcrumb">
      <el-breadcrumb separator="/" class="main-breadcrumb-wrap">
        <el-breadcrumb-item>
          <span class="main-breadcrumb-home">系统管理</span>
        </el-breadcrumb-item>
        <el-breadcrumb-item>
          <span class="main-breadcrumb-home">系统日志</span>
        </el-breadcrumb-item>
      </el-breadcrumb>
    </article>
    <div class="nx-content">
      <div class="flex-bet">
        <div class="left-title flex"></div>
        <el-form
          ref="searchForm"
          :inline="true"
          :model="searchForm"
          class="demo-form-inline"
        >
          <el-form-item label="">
            <el-select
              v-model="searchForm.type"
              placeholder="请选择操作类型"
              @change="onSubmit"
              clearable
            >
              <el-option label="后台管理" value="0"></el-option>
              <el-option label="可视化大屏" value="1"></el-option>
              <el-option
                v-if="userType == 0"
                label="定时任务"
                value="2"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="">
            <el-select
              v-if="userType == 0"
              v-model="searchForm.matchAreaId"
              placeholder="请选择赛区名称"
              clearable
              @change="onSubmit"
            >
              <el-option
                v-for="item in matchAreaArray"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="">
            <el-input
              maxlength="50"
              show-word-limit
              placeholder="请输入操作标题"
              v-model="searchForm.title"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="">
            <el-date-picker
              v-model="date"
              type="daterange"
              align="right"
              unlink-panels
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :picker-options="pickerOptions"
              value-format="yyyy-MM-dd"
              @change="handleDate"
              clearable
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit"
              >查询</el-button
            >
            <el-button @click="resetForm">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <NxTable
        :table-cols="tableCols"
        :table-data="tableData"
        :pag-ination="pagination"
        class="table-wrap"
        @refresh="refresh"
      >
        <template slot="type" slot-scope="scope">
          <span v-if="scope.row.type == 0">后台管理</span>
          <span v-if="scope.row.type == 1">可视化大屏</span>
        </template>
      </NxTable>
    </div>
  </div>
</template>
<script>
import NxTable from "@/components/NxTable.vue";
import { Page } from "@/utils/pagination";
import { getSysLogList } from "@/api/system";
import { getMatchAreaInfos } from "@/api/matchArea";

export default {
  components: {
    NxTable,
  },
  // 表格数据
  data() {
    return {
      // 查询字段
      searchForm: {
        pageNum: 1,
        pageSize: 10,
        type: "",
        matchAreaId: "",
        title: "",
        startDate: "",
        endDate: "",
      },
      // 表格字段
      tableCols: [
        {
          label: "赛区名称",
          prop: "matchAreaName",
          align: "center",
        },
        {
          label: "操作类型",
          slot: "type",
          align: "center",
        },
        {
          label: "操作标题",
          prop: "title",
          align: "center",
        },
        {
          label: "操作内容",
          prop: "content",
          align: "center",
        },
        {
          label: "操作人",
          prop: "username",
          align: "center",
        },
        {
          label: "操作时间",
          prop: "createDate",
          align: "center",
        },
      ],
      tableData: [],
      pagination: new Page(),
      date: [], // 日期范围
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      matchAreaArray: [], // 赛区集合
      userType: "", // 用户类型（0超级管理员，1赛区管理员）
      matchAreaId: "", // 赛区id
    };
  },
  // 初始化加载
  mounted() {
    this.userType = Number(window.localStorage.getItem("userType"));
    this.matchAreaId = Number(window.localStorage.getItem("matchAreaId"));
    if (this.userType == 1) {
      this.searchForm.matchAreaId = this.matchAreaId;
    }

    // 查询
    this.getList();

    // 获取赛区列表
    this.getMatchAreaInfos();
  },
  // 自定义方法
  methods: {
    // 查询
    onSubmit() {
      this.pagination.pageNum = 1;
      this.getList();
    },
    // 分页查询
    refresh(val) {
      this.searchForm.pageNum = val.pageNum;
      this.searchForm.pageSize = val.pageSize;
      this.getList();
    },
    // 重置
    resetForm() {
      this.searchForm = {
        pageNum: 1,
        pageSize: 10,
        type: "",
        matchAreaId: "",
        title: "",
        startDate: "",
        endDate: "",
      };

      if (this.userType == 1) {
        this.searchForm.matchAreaId = this.matchAreaId;
      }

      this.date = [];
      this.onSubmit();
    },
    // 查询列表
    getList() {
      getSysLogList(this.searchForm).then((res) => {
        if (res.code == 200) {
          this.tableData = res.data;
          this.pagination.count = res.count;
        }
      });
    },
    // 日期切换
    handleDate() {
      if (this.date && this.date.length == 2) {
        this.searchForm.startDate = this.date[0];
        this.searchForm.endDate = this.date[1];
      } else {
        this.searchForm.startDate = "";
        this.searchForm.endDate = "";
      }

      this.onSubmit();
    },
    // 获取赛区列表
    getMatchAreaInfos() {
      getMatchAreaInfos({}).then((res) => {
        if (res.code == 200) {
          this.matchAreaArray = res.data;
          this.matchAreaArray.unshift({ id: 0, name: "总部" });
        }
      });
    },
  },
};
</script>
<style lang="less" scoped>
.main-breadcrumb {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  height: 76px;
  border-bottom: solid 20px #f2f2f2;
  padding-left: 20px;
  .main-breadcrumb-wrap {
    font-size: 16px;
    line-height: 68px;
    .main-breadcrumb-home {
      cursor: pointer;
      color: #666666;
      font-weight: bold;
    }
    .main-breadcrumb-sp {
      color: #666666;
      font-weight: bold;
    }
  }
}
</style>